package com.vcredit.creditcat.web.app;

import com.alibaba.dubbo.config.annotation.Reference;
import com.vcredit.creditcat.api.ICustManager;
import com.vcredit.creditcat.api.IPrivilegeCatManager;
import com.vcredit.creditcat.dto.catCard.LotteryDto;
import com.vcredit.creditcat.dto.catfood.CustCatfoodLogDto;
import com.vcredit.creditcat.model.Cust;
import com.vcredit.creditcat.util.Pager;
import com.vcredit.creditcat.vo.FilterCatFoodInput;
import com.vcredit.creditcat.vo.FilterCatFoodOutput;
import com.vcredit.creditcat.vo.JsonResult;
import com.vcredit.framework.annotation.Log;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by liaoshengjun on 2017/12/11.
 */
@RestController
@RequestMapping("/privilegeCat")
public class PrivilegeCatController extends BaseCreditCatController{
	@Reference(version="1.0")
	private IPrivilegeCatManager privilegeCatManager;
	@Reference(version = "1.0")
	private ICustManager custManager;

	/**
	 * 获取抽奖数据
	 * @param request
	 * @return
	 */
	@Log
	@ResponseBody
	@RequestMapping("/getLotteryData")
	public JsonResult<LotteryDto> getLotteryData(HttpServletRequest request){
		LotteryDto result=privilegeCatManager.getLotteryData();
		return JsonResult.SuccessResponse(result);
	}

	/**
	 * 获取抽奖结果
	 * @param request
	 * @return
	 */
	@Log
	@ResponseBody
	@RequestMapping("/getLotteryResult")
	public JsonResult getLotteryResult(HttpServletRequest request) throws Exception{
		Cust cust = custManager.findById(super.getCust(request).getCustId());
		Integer prize=(Integer)privilegeCatManager.getLotteryResult(cust);
		Map<String,Object> result=new HashMap<>();
		result.put("prize",prize);
		return JsonResult.SuccessResponse(result);
	}

	/**
	 * 当前剩余多少猫粮
	**/
	@RequestMapping("/queryCustRemainCatfood")
	@Log
	public JsonResult<?> queryCustRemainCatfood(HttpServletRequest request) throws Exception {
		Cust cust = super.getCust(request);
		return JsonResult.SuccessResponse(privilegeCatManager.queryCustRemainCatfood(cust));
	}
	
	
	

	/**
	 * 校验猫粮当前是否已经签到过
	**/
	@RequestMapping("/canSignInToday")
	@Log
	public JsonResult<?> canSignInToday(HttpServletRequest request) throws Exception {
		Cust cust = super.getCust(request);
		if(privilegeCatManager.canSignInToday(cust)){
			return JsonResult.SuccessResponse(null);
		}
		return JsonResult.ErrorResponse(null);
	}

	/**
	 * 猫粮签到
	**/
	@RequestMapping("/signInToday")
	@Log
	public JsonResult<?> checkCatFoodSignData(HttpServletRequest request) throws Exception {
		Cust cust = super.getCust(request);
		if(privilegeCatManager.canSignInToday(cust)){
			if(privilegeCatManager.updateCustCatfood(1L, cust)){ //猫粮签到的类别(暂定1)
				return JsonResult.SuccessResponse(null);
			}
		}
		return JsonResult.ErrorResponse(null);
	}

	
	/**
	 * 猫粮数据分页查询
	 **/
	@RequestMapping("/queryCustCatfoodLog")
	@Log
	public FilterCatFoodOutput queryCustCatfoodLog(@RequestBody FilterCatFoodInput filter,HttpServletRequest request) throws Exception {
		Cust cust = super.getCust(request);			
		List<CustCatfoodLogDto> list=privilegeCatManager.queryCustCatfoodLog(cust);		
		Pager<CustCatfoodLogDto> page = new Pager<>(filter.getPageIndex(), filter.getPageSize(),list);		
		return new FilterCatFoodOutput(page.getDataList());
	}
	
	
	
}
